#include "Indenter.h"
#include "Method.h"

#include<iostream>
#include<string>
#include "Tokenizer.h"
#include<wchar.h>

using namespace std;

Indenter::Indenter(void)
{
	m_nIndent = 0;
	m_sIndent = "\t";
	m_bIgnoreWhitespace=true;
}

Indenter::~Indenter(void)
{

}

void Indenter::tabify(bool bEndOfScope=false)
{
    if(!bEndOfScope)
	{
		for(int i=0;i<m_nIndent;i++)
			cout<<m_sIndent;
	}
	else
	{
		for(int i=0;i<m_nIndent-1;i++)
			cout<<m_sIndent;
	}
}

void Indenter::offNewToken(Scope& inScope, Token& inToken)
{
	char cDelimiter = char(inToken);
	bool bDontPrint = false;

	if(cDelimiter == '\n')
	{
		cout<<"\n";
		m_bIgnoreWhitespace = true;
	}
	
	if(m_bIgnoreWhitespace)
	{
		switch(cDelimiter)
		{
		case ' ':
		case '\r':
		case '\n':
		case '\t':
			bDontPrint = true;
			break;
		
		default:
			m_bIgnoreWhitespace = false;
			switch(cDelimiter) //TODO Ugly 0999 Hack for scope enders
			{
			case ')':
			case ']':
			case '}':
				tabify(true);
				break;
			default:
				tabify();
				break;
			}
			break;
		}
	}

	if(!bDontPrint)
		cout<<inToken.m_sValue;
		
}


void Indenter::offScopeEntry(Scope& inNewScope, Scope& inOldScope)
{
	m_nIndent++;
}

void Indenter::offScopeExit(Scope& inNewScope, Scope& inOldScope)
{
	if(m_nIndent)
		m_nIndent--;

	if(inOldScope.getScopeType() == Scope::eScopeBlock)
	{
		if(inOldScope.m_sName != "")
			cout << "/* End of " << inOldScope.m_sName << " */ ";
	}
}

